home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / PRDOSFS.C < prev    next >
C/C++ Source or Header  |  1992-05-05  |  13KB  |  397 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/dos386/microcode/RCS/prdosfs.c,v 1.1 1992/05/05 06:55:13 jinx Exp $
  4.  
  5. Copyright (c) 1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Unix-specific file-system primitives. */
  36. /* DOS Immitation */
  37.  
  38. #include "scheme.h"
  39. #include "prims.h"
  40. #include "msdos.h"
  41. #include "osfs.h"
  42.  
  43. extern int EXFUN
  44.   (DOS_read_file_status, (CONST char * filename, struct stat * s));
  45.  
  46. static SCHEME_OBJECT EXFUN (file_attributes_internal, (struct stat * s));
  47. static void EXFUN (file_mode_string, (struct stat * s, char * a));
  48. static char EXFUN (file_type_letter, (struct stat * s));
  49. static void EXFUN (rwx, (unsigned short bits, char * chars));
  50. static SCHEME_OBJECT EXFUN (file_touch, (CONST char * filename));
  51. static void EXFUN (protect_fd, (int fd));
  52.  
  53. #ifndef FILE_TOUCH_OPEN_TRIES
  54. #define FILE_TOUCH_OPEN_TRIES 5
  55. #endif
  56.  
  57. DEFINE_PRIMITIVE ("FILE-MODES", Prim_file_modes, 1, 1,
  58.   "Return mode bits of FILE, as an integer.")
  59. {
  60.   struct stat stat_result;
  61.   PRIMITIVE_HEADER (1);
  62.   PRIMITIVE_RETURN
  63.     ((DOS_read_file_status ((STRING_ARG (1)), (&stat_result)))
  64.      ? (LONG_TO_UNSIGNED_FIXNUM ((stat_result . st_mode) & 07777))
  65.      : SHARP_F);
  66. }
  67.  
  68. DEFINE_PRIMITIVE ("SET-FILE-MODES!", Prim_set_file_modes, 2, 2,
  69.   "Set the mode bits of FILE to MODE.")
  70. {
  71.   PRIMITIVE_HEADER (2);
  72.   if ((DOS_chmod ((STRING_ARG (1)), (arg_index_integer (2, 010000)))) < 0)
  73.     error_system_call (errno, syscall_chmod);
  74.   PRIMITIVE_RETURN (SHARP_F);
  75. }
  76.  
  77. DEFINE_PRIMITIVE ("FILE-MOD-TIME", Prim_file_mod_time, 1, 1, 0)
  78. {
  79.   struct stat s;
  80.   PRIMITIVE_HEADER (1);
  81.   PRIMITIVE_RETURN
  82.     ((DOS_read_file_status ((STRING_ARG (1)), (&s)))
  83.      ? (long_to_integer (s . st_mtime))
  84.      : SHARP_F);
  85. }
  86.  
  87. DEFINE_PRIMITIVE ("FILE-MOD-TIME-INDIRECT", Prim_file_mod_time_indirect, 1, 1, 0)
  88. {
  89.   struct stat s;
  90.   PRIMITIVE_HEADER (1);
  91.   PRIMITIVE_RETURN
  92.     ((DOS_read_file_status ((STRING_ARG (1)), (&s)))
  93.      ? (long_to_integer (s . st_mtime))
  94.      : SHARP_F);
  95. }
  96.  
  97. /* Returns a vector of 10 items:
  98.  
  99.    0 = #T iff the file is a directory,
  100.        string (name linked to) for symbolic link,
  101.        #F for all other files.
  102.    1 = number of links to the file
  103.    2 = user id, as an unsigned integer
  104.    3 = group id, as an unsigned integer
  105.    4 = last access time of the file
  106.    5 = last modification time of the file
  107.    6 = last change time of the file
  108.    7 = size of the file in bytes
  109.    8 = mode string for the file
  110.    9 = inode number of the file
  111.  
  112.    The file_mode_string stuff was gobbled from GNU Emacs. */
  113.  
  114. #define FILE_ATTRIBUTES_PRIMITIVE(stat_syscall)                \
  115. {                                    \
  116.   struct stat s;                            \
  117.   PRIMITIVE_HEADER (1);                            \
  118.   PRIMITIVE_RETURN                            \
  119.     ((stat_syscall ((STRING_ARG (1)), (&s)))                \
  120.      ? (file_attributes_internal (&s))                    \
  121.      : SHARP_F);                            \
  122. }
  123.  
  124. DEFINE_PRIMITIVE ("FILE-ATTRIBUTES", Prim_file_attributes, 1, 1,
  125.   "Given a file name, return attribute information about the file.\n\
  126. If the file exists and its status information is accessible, the result\n\
  127. is a vector of 10 items (see the reference manual for details).  Otherwise\n\
  128. the result is #F.")
  129.      FILE_ATTRIBUTES_PRIMITIVE (DOS_read_file_status)
  130.  
  131. DEFINE_PRIMITIVE ("FILE-ATTRIBUTES-INDIRECT", Prim_file_attributes_indirect, 1, 1,
  132.   "Like FILE-ATTRIBUTES but indirect through symbolic links.")
  133.      FILE_ATTRIBUTES_PRIMITIVE (DOS_read_file_status)
  134.  
  135. static SCHEME_OBJECT
  136. DEFUN (file_attributes_internal, (s), struct stat * s)
  137. {
  138.   SCHEME_OBJECT result = (allocate_marked_vector (TC_VECTOR, 10, true));
  139.   SCHEME_OBJECT modes = (allocate_string (10));
  140.   switch ((s -> st_mode) & S_IFMT)
  141.     {
  142.     case S_IFDIR:
  143.       VECTOR_SET (result, 0, SHARP_T);
  144.       break;
  145. #ifdef S_IFLNK
  146.     case S_IFLNK:
  147.       VECTOR_SET (result, 0,
  148.           (char_pointer_to_string
  149.            ((unsigned char *)
  150.             (OS_file_soft_link_p
  151.              ((CONST char *) (STRING_LOC ((ARG_REF (1)), 0)))))));
  152.       break;
  153. #endif
  154.     default:
  155.       VECTOR_SET (result, 0, SHARP_F);
  156.       break;
  157.     }
  158.   VECTOR_SET (result, 1, (long_to_integer (s -> st_nlink)));
  159.   VECTOR_SET (result, 2, (long_to_integer (s -> st_uid)));
  160.   VECTOR_SET (result, 3, (long_to_integer (s -> st_gid)));
  161.   VECTOR_SET (result, 4, (long_to_integer (s -> st_atime)));
  162.   VECTOR_SET (result, 5, (long_to_integer (s -> st_mtime)));
  163.   VECTOR_SET (result, 6, (long_to_integer (s -> st_ctime)));
  164.   VECTOR_SET (result, 7, (long_to_integer (s -> st_size)));
  165.   file_mode_string (s, ((char *) (STRING_LOC (modes, 0))));
  166.   VECTOR_SET (result, 8, modes);
  167.   VECTOR_SET (result, 9, (long_to_integer (s -> st_ino)));
  168.   return (result);
  169. }
  170.  
  171. /* file_mode_string - set file attribute data
  172.  
  173.    File_mode_string converts the data in the st_mode field of file
  174.    status block `s' to a 10 character attribute string, which it
  175.    stores in the block that `a' points to.
  176.  
  177.    This attribute string is modelled after the string produced by the
  178.    Berkeley ls.
  179.  
  180.    As usual under Unix, the elements of the string are numbered from
  181.    0.  Their meanings are:
  182.  
  183.    0    File type.  'd' for directory, 'c' for character special, 'b'
  184.     for block special, 'm' for multiplex, 'l' for symbolic link,
  185.     's' for socket, 'p' for fifo, '-' for any other file type
  186.    1    'r' if the owner may read, '-' otherwise.
  187.    2    'w' if the owner may write, '-' otherwise.
  188.    3    'x' if the owner may execute, 's' if the file is set-user-id,
  189.     '-' otherwise.  'S' if the file is set-user-id, but the
  190.     execute bit isn't set.  (sys V `feature' which helps to catch
  191.     screw case.)
  192.    4    'r' if group members may read, '-' otherwise.
  193.    5    'w' if group members may write, '-' otherwise.
  194.    6    'x' if group members may execute, 's' if the file is
  195.     set-group-id, '-' otherwise.  'S' if it is set-group-id but
  196.     not executable.
  197.    7    'r' if any user may read, '-' otherwise.
  198.    8    'w' if any user may write, '-' otherwise.
  199.    9    'x' if any user may execute, 't' if the file is "sticky" (will
  200.     be retained in swap space after execution), '-' otherwise. */
  201.  
  202. static void
  203. DEFUN (file_mode_string, (s, a), struct stat * s AND char * a)
  204. {
  205.   (a[0]) = (file_type_letter (s));
  206.   rwx ((((s -> st_mode) & 0700) << 0), (& (a [1])));
  207.   rwx ((((s -> st_mode) & 0070) << 3), (& (a [4])));
  208.   rwx ((((s -> st_mode) & 0007) << 6), (& (a [7])));
  209. #ifdef S_ISUID
  210.   if (((s -> st_mode) & S_ISUID) != 0)
  211.     (a[3]) = (((a[3]) == 'x') ? 's' : 'S');
  212. #endif
  213. #ifdef S_ISGID
  214.   if (((s -> st_mode) & S_ISGID) != 0)
  215.     (a[6]) = (((a [6]) == 'x') ? 's' : 'S');
  216. #endif
  217. #ifdef S_ISVTX
  218.   if (((s -> st_mode) & S_ISVTX) != 0)
  219.     (a[9]) = (((a [9]) == 'x') ? 't' : 'T');
  220. #endif
  221. }
  222.  
  223. static char
  224. DEFUN (file_type_letter, (s), struct stat * s)
  225. {
  226.   switch ((s -> st_mode) & S_IFMT)
  227.     {
  228.     case S_IFDIR:
  229.       return ('d');
  230.     case S_IFCHR:
  231.       return ('c');
  232.     case S_IFBLK:
  233.       return ('b');
  234. #ifdef S_IFLNK
  235.     case S_IFLNK:
  236.       return ('l');
  237. #endif
  238. #ifdef S_IFMPC
  239. /* These do not seem to exist */
  240.     case S_IFMPC:
  241.     case S_IFMPB:
  242.       return ('m');
  243. #endif
  244. #ifdef S_IFSOCK
  245.     case S_IFSOCK:
  246.       return ('s');
  247. #endif
  248. #ifdef S_IFIFO
  249.     case S_IFIFO:
  250.       return ('p');
  251. #endif
  252. #ifdef S_IFNWK /* hp-ux hack */
  253.     case S_IFNWK:
  254.       return ('n');
  255. #endif
  256.     default:
  257.       return ('-');
  258.     }
  259. }
  260.  
  261. static void
  262. DEFUN (rwx, (bits, chars), unsigned short bits AND char * chars)
  263. {
  264.   (chars[0]) = (((bits & S_IREAD) != 0)  ? 'r' : '-');
  265.   (chars[1]) = (((bits & S_IWRITE) != 0) ? 'w' : '-');
  266.   (chars[2]) = (((bits & S_IEXEC) != 0)  ? 'x' : '-');
  267. }
  268.  
  269. DEFINE_PRIMITIVE ("FILE-TOUCH", Prim_file_touch, 1, 1,
  270.   "Given a file name, change the times of the file to the current time.\n\
  271. If the file does not exist, create it.\n\
  272. Both the access time and modification time are changed.\n\
  273. Return #F if the file existed and its time was modified.\n\
  274. Otherwise the file did not exist and it was created.")
  275. {
  276.   PRIMITIVE_HEADER (1);
  277.   PRIMITIVE_RETURN (file_touch ((CONST char *) (STRING_ARG (1))));
  278. }
  279.  
  280. static SCHEME_OBJECT
  281. DEFUN (file_touch, (filename), CONST char * filename)
  282. {
  283.   int fd;
  284.   transaction_begin ();
  285.   {
  286.     unsigned int count = 0;
  287.     while (1)
  288.       {
  289.     count += 1;
  290.     /* Use O_EXCL to prevent overwriting existing file. */
  291.     fd = (DOS_open (filename, (O_RDWR | O_CREAT | O_EXCL), MODE_REG));
  292.     if (fd >= 0)
  293.       {
  294.         protect_fd (fd);
  295.         transaction_commit ();
  296.         return (SHARP_T);
  297.       }
  298.     if (errno == EEXIST)
  299.       {
  300.         fd = (DOS_open (filename, O_RDWR, MODE_REG));
  301.         if (fd >= 0)
  302.           {
  303.         protect_fd (fd);
  304.         break;
  305.           }
  306.         else if ((errno == ENOENT) || (errno == ESTALE))
  307.           continue;
  308.       }
  309.     if (count >= FILE_TOUCH_OPEN_TRIES)
  310.       error_system_call (errno, syscall_open);
  311.       }
  312.   }
  313.   {
  314.     struct stat file_status;
  315.     STD_VOID_SYSTEM_CALL (syscall_fstat, (DOS_fstat (fd, (&file_status))));
  316.     if (((file_status . st_mode) & S_IFMT) != S_IFREG)
  317.       error_bad_range_arg (1);
  318.     /* CASE 3: file length of 0 needs special treatment. */
  319.     if ((file_status . st_size) == 0)
  320.       {
  321.     char buf [1];
  322.     (buf[0]) = '\0';
  323.     STD_VOID_SYSTEM_CALL (syscall_write, (DOS_write (fd, buf, 1)));
  324. #ifdef HAVE_TRUNCATE
  325.     STD_VOID_SYSTEM_CALL (syscall_ftruncate, (DOS_ftruncate (fd, 0)));
  326.     transaction_commit ();
  327. #else /* not HAVE_TRUNCATE */
  328.     transaction_commit ();
  329.     fd = (DOS_open (filename, (O_WRONLY | O_TRUNC), MODE_REG));
  330.     if (fd >= 0)
  331.       STD_VOID_SYSTEM_CALL (syscall_close, (DOS_close (fd)));
  332. #endif /* HAVE_TRUNCATE */
  333.     return (SHARP_F);
  334.       }
  335.   }
  336.   /* CASE 4: read, then write back the first byte in the file. */
  337.   {
  338.     char buf [1];
  339.     int scr;
  340.     STD_UINT_SYSTEM_CALL (syscall_read, scr, (DOS_read (fd, buf, 1)));
  341.     if (scr > 0)
  342.       {
  343.     STD_VOID_SYSTEM_CALL (syscall_lseek, (DOS_lseek (fd, 0, SEEK_SET)));
  344.     STD_VOID_SYSTEM_CALL (syscall_write, (DOS_write (fd, buf, 1)));
  345.       }
  346.   }
  347.   transaction_commit ();
  348.   return (SHARP_F);
  349. }
  350.  
  351. static void
  352. DEFUN (protect_fd_close, (ap), PTR ap)
  353. {
  354.   DOS_close (* ((int *) ap));
  355. }
  356.  
  357. static void
  358. DEFUN (protect_fd, (fd), int fd)
  359. {
  360.   int * p = (dstack_alloc (sizeof (int)));
  361.   (*p) = fd;
  362.   transaction_record_action (tat_always, protect_fd_close, p);
  363. }
  364.  
  365. DEFINE_PRIMITIVE ("SET-FILE-TIMES!", Prim_set_file_times, 3, 3,
  366.   "Change the access and modification times of FILE.\n\
  367. The second and third arguments are the respective times;\n\
  368. they are integers are the times in seconds since 00:00:00 GMT, Jan. 1, 1970\n\
  369. The file must exist and you must be the owner (or superuser).")
  370. {
  371.   PRIMITIVE_HEADER (3);
  372.   {
  373.     time_t times[2];
  374.     
  375.     times[0] = (time_t) arg_integer (2);
  376.     times[1] = (time_t) arg_integer (3);
  377.     STD_VOID_SYSTEM_CALL(syscall_utime, (utime ((STRING_ARG (1)), ×)));
  378.     PRIMITIVE_RETURN (SHARP_F);
  379.   }
  380. }
  381.  
  382. DEFINE_PRIMITIVE ("FILE-EQ?", Prim_file_eq_p, 2, 2,
  383.   "True iff the two file arguments are the same file.")
  384. {
  385.   PRIMITIVE_HEADER (2);
  386.   {
  387.     struct stat s1;
  388.     struct stat s2;
  389.     PRIMITIVE_RETURN
  390.       (BOOLEAN_TO_OBJECT
  391.        ((DOS_read_file_status ((STRING_ARG (1)), (&s1)))
  392.     && (DOS_read_file_status ((STRING_ARG (2)), (&s2)))
  393.     && ((s1 . st_dev) == (s2 . st_dev))
  394.     && ((s1 . st_ino) == (s2 . st_ino))));
  395.   }
  396. }
  397.